package com.xuebank.nfe.nfecar.utils.loading

import android.content.Context
import android.graphics.drawable.Drawable
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.wzn.libaray.utils.Logger
import com.xuebank.nfe.nfecar.R


/**
 * 全局加载页面
 * 1.暂未扩展：更换加载动画
 */
class PromptView : RelativeLayout {
    private val TAG = javaClass.simpleName

    val mRootView: LinearLayout by lazy {
        findViewById<LinearLayout>(R.id.root_view)
    }

    val mProgress: ProgressBar by lazy {
        findViewById<ProgressBar>(R.id.common_loadding_progress)
    }

    val mLoadingImage: ImageView by lazy {
        findViewById<ImageView>(R.id.common_loading_image)
    }

    val mLoadingText: TextView by lazy {
        findViewById<TextView>(R.id.common_loading_text)
    }
    val mLoadingBtn: TextView by lazy {
        findViewById<TextView>(R.id.common_loading_button)
    }

    internal var size: Float = 0.toFloat()
    //设置加载动图(有动图有限加载动图)
    var mLoadingAnimImageResId: Int? = R.mipmap.ic_loading
    var mLoadingAnimDrawable: GifDrawable? = null

    //    设置加载静图(有动图有限加载动图)
    var mLoadingImageResId: Int? = 0
    var mLoadingDrawable: Drawable? = null
        get() {
            if (mLoadingImageResId != 0)
                return ContextCompat.getDrawable(context, mLoadingImageResId!!)
            else
                return field
        }

    var mLoadingSucessImageResId: Int? = 0
    var mLoadingSucessDrawable: Drawable? = null
        get() {
            if (mLoadingSucessImageResId != 0)
                return ContextCompat.getDrawable(context, mLoadingSucessImageResId!!)
            else
                return field
        }

    var mLoadingFailedImageResId: Int? = R.mipmap.ic_network_failed
    var mLoadingFailedDrawable: Drawable? = null
        get() {
            if (mLoadingFailedImageResId != 0)
                return ContextCompat.getDrawable(context, mLoadingFailedImageResId!!)
            else
                return field
        }

    var mLoadingEmptyImageResId: Int? = R.mipmap.ic_loading_failed
    var mLoadingEmptyDrawable: Drawable? = null
        get() {
            if (mLoadingEmptyImageResId != 0)
                return ContextCompat.getDrawable(context, mLoadingEmptyImageResId!!)
            else
                return field
        }

    var mLoadingTxt: String? = "加载中，请稍候……"
    var mLoadingFailedTxt: String? = "网络好像被外星人劫持了\n您可以点击重试或退出APP再次进入"
    var mLoadingFailedBtnTxt: String? = "点击重试"
    var mLoadingEmptyTxt: String? = "暂无数据"

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init()
    }

    private fun init() {
        View.inflate(context, R.layout.layout_loading, this)
        size = mLoadingText.textSize
        visibility = View.GONE
    }

    private fun startAnim(): PromptView? {
        if (mLoadingAnimImageResId == 0) {
            mProgress.visibility = View.VISIBLE
            mLoadingImage.visibility = View.GONE
        } else {
            mProgress.visibility = View.GONE
            mLoadingImage.visibility = View.VISIBLE
            Glide.with(context).load(mLoadingAnimImageResId).into(mLoadingImage)
            mLoadingAnimDrawable = mLoadingImage.drawable as GifDrawable?
            if (mLoadingAnimDrawable?.isRunning != false)
                mLoadingAnimDrawable?.start()
        }

        return this
    }

    private fun stopAnim(): PromptView? {
        if (mLoadingImage.visibility == View.VISIBLE) {
            val gifDrawable = mLoadingImage.drawable
            if (null != gifDrawable && gifDrawable is GifDrawable) {
                gifDrawable.stop()
            }
        }

        if (mProgress.visibility == View.VISIBLE) {
            mProgress.visibility = View.GONE
        }
        return this
    }

    fun showLoading(): PromptView? {
        Logger.d(TAG, "showLoading……")
        this.visibility = View.VISIBLE
        startAnim()

        mLoadingText.text = mLoadingTxt

        mLoadingBtn.visibility = View.GONE

        return this
    }

    fun onLoadingSuccess(): PromptView? {
        Logger.d(TAG, "onLoadingSuccess……")
        this.visibility = View.GONE

        stopAnim()
        return this
    }

    fun onLoadingFailed(failedAction: (() -> Unit)? = null): PromptView? {
        Logger.d(TAG, "onLoadingFailed……")
        if (visibility == View.GONE) visibility = View.VISIBLE

        stopAnim()

        if (mLoadingFailedImageResId == 0) {
            mLoadingImage.visibility=View.INVISIBLE
        } else {
            mLoadingImage.visibility=View.VISIBLE
            Glide.with(context).load(mLoadingFailedImageResId).into(mLoadingImage)
        }

        mLoadingText.text = mLoadingFailedTxt
        mLoadingText.visibility = View.VISIBLE
        mLoadingBtn.visibility = View.VISIBLE
        mLoadingBtn.text = mLoadingFailedBtnTxt

        mLoadingBtn.setOnClickListener {
            failedAction?.invoke()
        }

        return this
    }

    /**
     * @param resId 0 不显示图片
     * *
     * @param msg   null 为默认提示
     * *
     * @return
     */
    fun onLoadingEmpty(btnTxt: String? = null, emptyAction: (() -> Unit)? = null): PromptView? {
        Logger.d(TAG, "onLoadingEmpty……")
        if (visibility == View.GONE) visibility = View.VISIBLE

        mLoadingText.visibility = View.VISIBLE
        mLoadingBtn.visibility = View.GONE

        stopAnim()

        if (mLoadingEmptyDrawable == null) {
            mLoadingImage.visibility = View.GONE
            val params = mLoadingText.layoutParams as ViewGroup.LayoutParams
            params.width = ViewGroup.LayoutParams.MATCH_PARENT
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT
            mLoadingText.gravity = Gravity.CENTER_HORIZONTAL
            mLoadingText.layoutParams = params
        } else {
            mLoadingImage.visibility = View.VISIBLE
            mLoadingImage.setImageDrawable(mLoadingEmptyDrawable)
        }

        mLoadingText.text = mLoadingEmptyTxt

        if (emptyAction == null) {
            mLoadingBtn.visibility = View.GONE
            mLoadingBtn.setOnClickListener(null)
        } else {
            mLoadingBtn.visibility = View.VISIBLE
            mLoadingBtn.text = btnTxt
            mLoadingBtn.setOnClickListener {
                emptyAction.invoke()
            }
        }

        return this
    }

    override fun onTouchEvent(event: MotionEvent): Boolean = true
}
